﻿using DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class ThamSoDAO
    {
        private MyConnection myConnection;

        public ThamSoDAO()
        {
            myConnection = DataAccessObject.GetDAL();
        }

        public ThamSoDTO GetThamSo()
        {
            SqlDataReader reader = this.myConnection.Select("dbo.THAMSO", null, null);
            ThamSoDTO thamSo = null;
            if (reader.Read())
            {
                thamSo = Convert((IDataReader)reader);
            }
            myConnection.Disconnect();
            return thamSo;
        }

        public void Update(ThamSoDTO thamSo)
        {
            List<DataStruct> dataStruct = new List<DataStruct>();
            dataStruct.Add(new DataStruct("TLungToiDa", thamSo.TLUng));
            dataStruct.Add(new DataStruct("GTHDToiDa", thamSo.GTHD));
            dataStruct.Add(new DataStruct("SLNhapToiDa", thamSo.SLNhap));
            dataStruct.Add(new DataStruct("SLTonToiDa", thamSo.SLTon));
            this.myConnection.Update("dbo.THAMSO", dataStruct, null);
        }

        private ThamSoDTO Convert(IDataRecord dataRecord)
        {
            ThamSoDTO thamSo = new ThamSoDTO();
            thamSo.TLUng = double.Parse(dataRecord["TLungToiDa"].ToString());
            thamSo.GTHD = int.Parse(dataRecord["GTHDToiDa"].ToString());
            thamSo.SLNhap = int.Parse(dataRecord["SLNhapToiDa"].ToString());
            thamSo.SLTon = int.Parse(dataRecord["SLTonToiDa"].ToString());
            return thamSo;
        }
    }
}